//app.js
import {
  login,
  getUnRead
} from './api/user';
import obj from './config'
App({
  onLaunch: function () {
    
    // wx.request({
    //   url: obj.api+'sys/login?code=asfas',
    //   method:"POST",
    //   success: data => {
    //     console.log(data)
    //   }
    // })
    this.listener={
      promise:()=>{
        return new Promise((resolve,reject)=>{
          this.listener.resolve = resolve
        })
      },
      resolve:null
    }
    // 更新小程序
    const upm = wx.getUpdateManager();
    if (upm) {
      upm.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
            if (res.confirm) {
              upm.applyUpdate()
            }
          },
          fail: function (err) {
            console.warn(err);
          }
        })
      })
    }

    /** 获取设备信息 */
    wx.getSystemInfo({
      success: result => {
        // console.log(result)
        let {
          brand,
          model,
          language,
          version,
          system,
          platform,
          fontSizeSetting,
          SDKVersion,
          windowWidth,
        } = result;
        this.globalData.isIOS = system.includes('OS');
        this.globalData.systemInfo = {
          version,
          model,
          brand,
          platform,
          system,
          SDKVersion,
          fontSizeSetting,
          language,
          px2rpx: 750 / windowWidth,
          rpx2px: windowWidth / 750,
        };
      },
    })

    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框

          // 静默登录
          // wx.login({
          //   success: res => {
          //     let code = res.code;

          //     wx.getUserInfo({
          //       success: res => {
          //          console.log( '用户信息info',res)
          //         // 可以将 res 发送给后台解码出 unionId
          //         this.globalData.userInfo = res.userInfo
                  

          //         // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          //         // 所以此处加入 callback 以防止这种情况
          //         if (this.userInfoReadyCallback) {
          //           this.userInfoReadyCallback(res)
          //         }

          //         // 静默登录
                  
          //       }
          //     })
          //   }
          // })
        }
      }
    })
  },
  globalData: {
    userInfo: {
      avatarUrl: "",
      nickName: "",
      mobile: '18819213121',
      city: "",
      country: "",
      gender: 0,
      language: "",
      province: "",
      companyId:0,
    },
    token: '',
    memberId: 0,
    openid: 0,
    isLogin: false,
    isIOS: false,
    systemInfo: {},
    isOpen:'',
    roleName:'',
    roleId:'',
    unRead: {
      certificate: [],
      company: [],
      invite: {
        members: [],
        vips: []
      },
      invoice: [],
      school: {
        fail: [],
        pass: [],
        pending: []
      }
    }, // 红点
  }
})